﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;

using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;
using SIG.Model.Jobs;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class JobService: EntityService<Job>, IJobService
    {
     
        private readonly IJobRepository _jobRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public JobService(IJobRepository jobRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(jobRepository, cacheService, loggingService)
        {
           
            _jobRepository = jobRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public IEnumerable<Job> GetActiveJobs()
        {
            //get list
            var key = $"{EntityNames.Job}_ActiveList";

            List<Job> jobs;
            if (SettingsManager.Job.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    jobs = (List<Job>)_cacheService.Get(key);
                }
                else
                {
                    jobs = _jobRepository.GetFilteredElements(c=>c.Active).ToList();
                    _cacheService.Set(key, jobs, SettingsManager.Job.CacheDuration);
                }
            }
            else
            {
                jobs = _jobRepository.GetFilteredElements(c => c.Active).ToList();
            }

            return jobs;
        }


        public List<Job> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Job}_ListCount_{keyword}";

            var totalIQuery = _jobRepository.GetAllElements();
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Post.Contains(keyword));
         

            if (SettingsManager.Job.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Job.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Job}_List_{pageIndex}_{pageSize}_{keyword}";

            List<Job> job;
            Expression<Func<Job, bool>> filter = g => true;
            Expression<Func<Job, bool>> filterByKeyword = g => g.Post.Contains(keyword);
         

            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);
           


            if (SettingsManager.Job.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    job = (List<Job>)_cacheService.Get(key);

                }
                else
                {
                    job = _jobRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
                    _cacheService.Set(key, job, SettingsManager.Job.CacheDuration);
                }
            }
            else
            {
                job = _jobRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
            }

            return job;

        }

        public IEnumerable<Job> LatesterJobs(int count)
        {
            var key = $"{EntityNames.Job}s_Latester_{count}";

            if (!SettingsManager.Job.EnableCaching)
            {
                return _jobRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            }

            if (_cacheService.IsSet(key))
            {
                return (List<Job>)_cacheService.Get(key);

            }

            var jobList = _jobRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            _cacheService.Set(key, jobList, SettingsManager.Job.CacheDuration);


            return jobList;
        }
    }
}
